
#include <gio/gio.h>
#include <string.h>
#include <libsoup/soup.h>
#define SOFTWARE_NAME "influxdb"
//create database:
#define CREATE_TABLE "http://127.0.0.1:8086/query?q=create database history1"
#define WRITE_DATA "http://127.0.0.1:8086/write?db=history&precision=s"
//history,id=23 value='3 2',status=1"
#define SELECT_DATA "http://127.0.0.1:8086/query?db=history&q=select * from history"
//#define DELETE_DATA "http://127.0.0.1:8086/query?db=history&q=ALTER hisotry DROP value1"
/*
{
    "results": [
        {
            "statement_id": 0,
            "series": [
                {
                    "name": "history",
                    "columns": [
                        "time",
                        "id",
                        "status",
                        "value"
                    ],
                    "values": [
                        [
                            "2023-09-17T08:00:00Z",
                            "23",
                            1,
                            32
                        ],
                        [
                            "2023-09-18T07:55:00Z",
                            "23",
                            1,
                            32
                        ]
                    ]
                }
            ]
        }
    ]
}
*/
gboolean write_data (const gchar *db, const gchar *data)
{
  gboolean rc = FALSE;
  SoupSession* session = soup_session_new();
  SoupMessage* msg = NULL;

  msg = soup_message_new(SOUP_METHOD_POST, db);
  if (data) {
    int data_len = strlen (data);
    soup_message_set_request(msg, "text/plain",
        SOUP_MEMORY_COPY, data, data_len);
  }
  soup_session_send_message(session, msg);
  g_print("status code: %d\t", msg->status_code);
  g_print("body: %s\n", msg->response_body->data);
  if (msg->status_code == 204)
    rc = TRUE;
  goto out;
out:
  g_object_unref(msg);
  g_object_unref(session);
  return rc;
}
int main(int argc, char **argv)
{
  gint rc = 0;
  g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);
  g_set_prgname (SOFTWARE_NAME);

  write_data (CREATE_TABLE, NULL);
  write_data (WRITE_DATA, "history,id=23 value1=\"3 2!!!!!!!!!!!!!!!!!\",status=1\nhistory,id=24 value=32,status=1");
  write_data (SELECT_DATA, NULL);
  //write_data (DELETE_DATA, NULL);
  write_data (SELECT_DATA, NULL);

  goto out;
out:
	return rc;
}
